package handler

import (
	"encoding/json"
	"github.com/xiyoufang/cloud-syncer/internal/env"
	"github.com/xiyoufang/cloud-syncer/internal/http/model"
	"net/http"
	"strings"
)

func LogoutHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	tokenString := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
	env.Cache().Delete(tokenString)
	response := model.ResponseBodyEntity{
		Code:    "OK",
		Message: "success",
	}
	if err := json.NewEncoder(w).Encode(response); err != nil {
		env.Logger().Error(err.Error())
		http.Error(w, "Failed to encode response", http.StatusInternalServerError)
		return
	}
}
